package com.example.controller;

import com.example.school.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class RedisClientController {
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/setVal/{val}")
    public String setVal(@PathVariable("val") String val) {
        redisTemplate.boundValueOps("hello").set(val);

        return "ok";
    }

    @GetMapping("/getVal/{key}")
    public Object getVal(@PathVariable("key") String key) {
        return redisTemplate.boundValueOps(key).get();
    }

    @GetMapping("/setUser")
    public String setUser(Student student) {
        System.out.println(student);
        redisTemplate.boundHashOps("student").put(student.getName(), student);
        return "ok";
    }

    @GetMapping("/getUsers")
    public List<Student> getUsers() {
        return redisTemplate.boundHashOps("student").values();
    }
}
